package com.itheima;

import com.itheima.config.SpringConfig;
import com.itheima.dao.AccountDao;
import com.itheima.dao.BookDao;
import com.itheima.domain.Account;
import com.itheima.service.AccountService;
import com.itheima.service.BookService;
import com.itheima.service.ResourcesService;
import com.itheima.service.impl.BookServiceImpl;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.sql.DataSource;
import java.io.InputStream;

public class App {
/*    public static void main(String[] args) {
        BookService bookService = new BookServiceImpl();
        bookService.save();
    }*/

    /* public static void main(String[] args) {
       //3.获取IoC容器
       ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
       //4.获取bean（根据bean配置id获取）
//        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
//        bookDao.save();

       BookService bookService = (BookService) ctx.getBean("bookService");
       bookService.save();

   }*/

    /* public static void main(String[] args) {
       ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
       DataSource dataSource = (DataSource) ctx.getBean("dataSource");
       System.out.println(dataSource);
   }*/

    /*//读取单个属性
    //从IOC容器中获取bookDao对象，调用方法，查看值是否已经获取到并打印控制台
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        bookDao.save();
    }*/
/*
    //核心容器
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        //bean的三种获取方式
        //方式一
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        //方式二:解决强转问题，但是参数又多加了一个，相对来说没有简化多少
        //BookDao bookDao = ctx.getBean("bookDao", BookDao.class);
        //方式三：这种方式就类型我肯之前所学习依赖注入的按类型注入，必须确保IOC容器中该类型对应的bean对象只能有一个
        //BookDao bookDao = ctx.getBean(BookDao.class);
        bookDao.save();
    }*/

   /* //注解开发定义bean
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        BookDao bookDao = (BookDao) ctx.getBean("bookDao");
        System.out.println(bookDao);
        //按类型获取bean
        BookService bookService = ctx.getBean(BookService.class);
        System.out.println(bookService);
    }*/

   /* //注解开发bean作用范围与生命周期
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao1 = ctx.getBean(BookDao.class);
        BookDao bookDao2 = ctx.getBean(BookDao.class);
        System.out.println(bookDao1);
        System.out.println(bookDao2);
        ctx.close();//关闭容器
    }*/

   /* //注解开发依赖注入
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookService bookService = ctx.getBean(BookService.class);
        bookService.save();
    }*/
/*
    //IOC/DI注解开发管理第三方bean
    public static void main(String[] args) {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        DataSource dataSource = ctx.getBean(DataSource.class);
        System.out.println(dataSource);
    }*/

   /* //Spring整合mybatis
    public static void main(String[] args) throws Exception{
        //1.创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        //2.创建SqlMapConfig.xml配置文件
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //3.创建SqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
        //4.获取SqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //5.执行SqlSession对象执行查询,获取结果User
        AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
        Account ac = accountDao.findById(1);
        System.out.println(ac);
        //6.释放资源
        sqlSession.close();
    }*/

    /*//Spring整合mybatis,congIOC容器中获取Service对象，调用方法获取结果
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        AccountService accountService = ctx.getBean(AccountService.class);
        Account ac = accountService.findById(1);
        System.out.println(ac);
    }*/

    /*//AOP入门案例
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
        //bookDao.update();
        //验证容器中是否为代理对象
        System.out.println(bookDao);
        System.out.println(bookDao.getClass());
    }*/

    /*//AOP通知类型
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
        int num = bookDao.select();
        System.out.println(num);
    }*/

  /*  //AOP通知获取数据
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        BookDao bookDao = ctx.getBean(BookDao.class);
        String name = bookDao.findName(100,"itheima");
        System.out.println(name);
    }*/

   /* //百度网盘密码数据兼容处理
    public static void main(String[] args) {
        ApplicationContext ctx = new AnnotationConfigApplicationContext(SpringConfig.class);
        ResourcesService resourcesService = ctx.getBean(ResourcesService.class);
        boolean flag =  resourcesService.openUrl("http://pan.baidu.com/haha","1234567");
        System.out.println(flag);
    }*/
}
